package cn.lijiguo.concurrency.part1.ch06;

import java.util.concurrent.TimeUnit;

/**
 * RightWayStopThreadWithSleep
 * run 方法中 调用 sleep() 方法时 中断线程的写法
 * @author lijiguo
 * @date 2020/1/6 10:55
 */
public class RightWayStopThreadWithSleep {

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
            while (num <= 300 && !Thread.currentThread().isInterrupted()) {
                if (num % 100 == 0) {
                    System.out.println(num + " 是 100 的倍数");
                }
                num++;
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Thread t1 = new Thread(runnable, "t1");
        t1.start();

        // 睡眠 100 毫秒
        TimeUnit.MILLISECONDS.sleep(100);

        // 发出中断信号
        t1.interrupt();
    }

}
